from flask import render_template, request, url_for
from app.admin import admin
from app.api.API_use import get_pile_states, get_cars_message, get_pile_reports, off_pile, open_pile



@admin.route('/<username>')
def admin_interface(username):
    return render_template('admin/admin.html', current_admin=username)


@admin.route('/states')
def states_interface():
    states = get_pile_states()
    return render_template('admin/states.html', result=states)


@admin.route('/carsmsg')
def carsmsg_interface():
    carsmsg = get_cars_message()
    return render_template('admin/carsmsg.html', result=carsmsg)


@admin.route('/reports')
def reports_interface():
    reports = get_pile_reports()
    return render_template('admin/reports.html', result=reports)


@admin.route('/result', methods=['POST', 'GET'])
def new_states_interface():
    if request.method == 'POST':
        received = request.form
        received_list = list()
        for value in received.values():
            received_list.append(value)
        if received_list[1] == 'ON':
            open_pile(int(received_list[0]))
        if received_list[1] == 'OFF':
            off_pile(int(received_list[0]))
        states = get_pile_states()
        return render_template("admin/states.html", result=states)
